$(document).ready(function () {
    //$('.btn-group-sm .btn-outline-info').siblings('.btn-outline-warning').hide();
    var istype=$('#tbody').attr('istype');
    if(istype==0){var con='tables.html';}
    else{         var con='tables_lh.html';}
    $.ajax({
        url: "/Admin/Hold/listHistory",
        type: "post",
        cache: false,
        data: {
            type: istype
        },
        dataType: "json",
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        success: function (req) {
            // console.log(req.content)
            var str = ''
            for (var i = 0; i < req.content.length; i++) {
                // console.log(req.content[i].status)
                if (req.content[i].status == 0 &&req.content[i].stop_time == 0) {
                    str = `<tr class="code-add">
                  <td class="title"><span>${req.content[i].period_number}</span>期</td>
                  <td class="state">
                     未开始
                  </td>
                  <td>
                      <div class="btn-group btn-group-sm">
                      <button type="button" class="btn btn-start btn-outline-info"><i class="fa icon-btn fa fa-play-circle-o"></i>开盘</button>
                      </div>
                  </td>
              </tr>`
                    $('#tbody').html(str)
                }
                if (req.content[i].status == 1) {
                    str = ` <tr class="code-add">
                            <td class="title"><span>${req.content[i].period_number}</span>期</td>
                            <td class="state">
                             进行中
                            </td>
                            <td>
                            <div class="btn-group btn-group-sm">
                            <button type="button" class="btn btn-stop btn-outline-danger"><i class="fa icon-btn fa-stop-circle-o"></i>封盘</button>
                            <button type="button" class="btn btn-outline-info zy_btn_fpJs"><i class="fa icon-btn fa-stop-circle-o"></i>倒计时</button>
                            <!--<button type="button" class="btn btn-insert btn-outline-warning"><i class="fa icon-btn fa-pencil-square-o"></i>录入</button>-->
                            <!--<button type="button" class="btn btn-insert btn-outline-warning"><i class="fa icon-btn fa-pencil-square-o"></i>录入</button>-->
                        </div>
                            </td>
                        </tr>`;
                    $('#tbody').html(str);
                }else if (req.content[i].status == 0&&req.content[i].stop_time != 0) {
                    str = ` <tr class="code-add">
                            <td class="title"><span>${req.content[i].period_number}</span>期</td>
                            <td class="state">
                             已封盘
                            </td>
                            <td>
                            <div class="btn-group btn-group-sm">
                            <!--<button type="button" class="btn btn-stop btn-outline-danger"><i class="fa icon-btn fa-stop-circle-o"></i>封盘</button>-->
                            <!--<button type="button" class="btn btn-outline-info zy_btn_fpJs"><i class="fa icon-btn fa-stop-circle-o"></i>倒计时</button>-->
                            <button type="button" class="btn btn-insert btn-outline-warning"><i class="fa icon-btn fa-pencil-square-o"></i>录入</button>
                            <!--<button type="button" class="btn btn-insert btn-outline-warning"><i class="fa icon-btn fa-pencil-square-o"></i>录入</button>-->
                        </div>
                            </td>
                        </tr>`;
                    $('#tbody').html(str);
                }
                if (req.content[i].status == 2) {
                    //修改按钮
                    var xiugai=`<div class="btn-group btn-group-sm">
                                   <button type="button" class="btn btn-update btn-outline-warning" qihao="${req.content[i].period_number}"><i class="fa icon-btn fa-pencil-square-o"></i>修改</button>
                               </div>`;
                    var xiugai=``;
                    str += `        
                           <tr class="code-dd">
                           <td class="title"><span>${req.content[i].period_number}</span>期</td>
                           <td class="state">已结束</td>
                           <td>
                               `+xiugai+`
                           </td>
                       </tr>
                           `
                    $('#tbody').html(str)
                }
            }
            $('.btn-start').click(function () {
                var period_number = $(this).parents('.code-add').find('span').html();
                var type = istype;
                $.ajax({
                    url: "/Admin/Hold/start",
                    type: "post",
                    cache: false,
                    data: {
                        period_number: period_number,
                        type: type,
                    },
                    dataType: "json",
                    contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                    success: function (req) {
                        //   console.log(req.msg)
                        parent.layer.msg(req.msg, {
                            icon: 3,
                            title: '提示'
                        });
                        window.location.reload()
                    },
                    //请求出错处理。注意：有差错时才执行程序体，才会出现alert弹框
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert('a')
                    },
                })
            });
            $('.btn-update').click(function () {//点击修改
                layer.open({
                    title: '修改',
                    content: con+'?period_number=' + $('.btn-update').attr('qihao'),
                    type: 2,
                    area: ['800px', '400px']
                });
            });
            var fpPd=false;
            $('.zy_btn_fpJs').click(function(){
                if(fpPd)return;
                var thi=$(this);
                var period_number = $(this).parents('.code-add').find('span').html()
                var state = $(this).parents('.code-add').find('.state')
                var type=istype;
                $.ajax({
                    url: "/Admin/Hold/tipStop",
                    type: "post",
                    cache: false,
                    data: {
                        period_number: period_number,
                        type: type,
                    },
                    contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                    success: function (req) {
                        let i=10;
                        thi.html(i);
                        i--;
                        fpPd=true;
                        let time=setInterval(()=>{
                            if(i<=0){
                                fpPd=false;
                                thi.html('倒计时');
                                clearInterval(time);
                                return;
                            }
                            thi.html(i);
                            i--;
                        },1000);
                    },
                    //请求出错处理。注意：有差错时才执行程序体，才会出现alert弹框
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert('a')
                    },
                })
            });
            $('.btn-stop').click(function () {
                var thi=$(this);
                var period_number = $(this).parents('.code-add').find('span').html()
                var state = $(this).parents('.code-add').find('.state')
                var type = istype;
                clearInterval(window.zxTime);
                function ajaxStart(){
                    $.ajax({
                        url: "/Admin/Hold/stop",
                        type: "post",
                        cache: false,
                        data: {
                            period_number: period_number,
                            type: type,
                        },
                        dataType: "json",
                        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
                        success: function (req) {
                            if (req.msg == '封盘成功') {
                                var str1 = `<button type="button" class="btn btn-insert btn-outline-warning"><i class="fa icon-btn fa-pencil-square-o"></i>录入</button>`
                                $('.btn-stop').parent().html(str1)
                                state.html('已关闭')
                                $('.btn-stop').hide()
                            }
                        },
                        //请求出错处理。注意：有差错时才执行程序体，才会出现alert弹框
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert('a')
                        },
                    })
                }
                ajaxStart();
            });
            $('.btn-group').on('click','.btn-insert',function(){//录入弹窗
                var period_number = $(this).parents('.code-add').find('span').html()
                           var close = layer.open({
                                title: '录入',
                                content: con+'?period_number=' + period_number,
                                type: 2,
                                area: ['800px', '400px']
                            });
            });
            var period_number = getQueryString("period_number");


            function getQueryString(name) {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
                console.log(reg);
                var r = window.location.search.substr(1).match(reg);
                console.log(r);
                if (r != null) return unescape(r[2]);
                return null;
            }
            $('.btn-del').click(function () {
                layer.confirm('确定要删除吗?', {
                    icon: 2,
                    title: '提示'
                }, function (index) {
                    layer.msg('删除')
                });
            })

        },
        //请求出错处理。注意：有差错时才执行程序体，才会出现alert弹框
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert('a')
        },
    })

})